package com.gss.stockLedger.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gss.stockLedger.domain.StockTransactionDtls;
import com.gss.stockLedger.util.ApplicationConstants;

@Repository
public class QueryStockDAO {
	
	@Autowired
	SessionFactory sessionFactory;
	
	@Autowired
	SessionFactory factory2;
	
	public List<StockTransactionDtls> getStockTransactionsForProduct(Date endDate,String partId,String inventoryType)throws Exception
	{
		Session session = null;
		try{
		 session =  sessionFactory.openSession();
		Order order = Order.asc("transactionDate");
		Order order2 = Order.asc("assignmentId");
		Order order3 = Order.desc("transactionType");
		System.out.println("&"+partId+"&"+inventoryType+"&");
		Criteria criteria = session.createCriteria(StockTransactionDtls.class).add(Restrictions.eq("partId", partId)).add(Restrictions.eq("inventoryType", inventoryType)).addOrder(order).addOrder(order2).addOrder(order3);
		if(endDate!=null)
			criteria.add(Restrictions.le("transactionDate", endDate));
		return criteria.list();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally
		{
			if(session!=null&&session.isOpen())
			{
				session.close();
				//session = null;
			}
		}
		//DetachedCriteria detachedCriteria = DetachedCriteria.forClass(StockTransactionDtls.class);
		//detachedCriteria.add(Restrictions.eq("partId", "RNABC005")).addOrder(order)
		//return session.;
	}
	
	public List getUniquePartIds(String invType,String partId){
		Session session = null;
		try{
		 session =  sessionFactory.openSession();
		//Transaction transaction = session.beginTransaction();
		ProjectionList projectionList = Projections.projectionList();
		Projection projection = Projections.distinct(Projections.property("partId"));
		projectionList.add(projection);
		projectionList.add(Projections.property("inventoryType"));
		//projectionList.add(Projections.property("partDesc"));
		//Criteria criteria = session.createCriteria(StockTransactionDtls.class).setProjection(projectionList).add(Restrictions.eq("partId", "284B2JX51C"));
		//Criteria criteria = session.createCriteria(StockTransactionDtls.class).setProjection(projectionList).add(Restrictions.gt("partId", "769522383R"));
		//Criteria criteria = session.createCriteria(StockTransactionDtls.class).setProjection(projectionList).add(Restrictions.eq("inventoryType", "Steel-Trading"));//.add(Restrictions.gt("partId", "850227419R"));
		Criteria criteria = session.createCriteria(StockTransactionDtls.class).setProjection(projectionList);
		if(invType!=null&&!invType.equalsIgnoreCase("All"))
		{
			criteria.add(Restrictions.eq("inventoryType", invType));
		}
		if(partId!=null&&!partId.equalsIgnoreCase("All"))
		{
			criteria.add(Restrictions.gt("partId", partId));
		}
			//.add(Restrictions.gt("partId", "868859W57A"));
		//Criteria criteria = session.createCriteria(StockTransactionDtls.class).setProjection(projectionList).add(Restrictions.eq("inventoryType", "DM"));
		return criteria.list();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally{
			if(session!=null&&session.isOpen())
				{
					session.close();
					//session = null;
				}
		}
	}
	
	public long uploadStock(List<StockTransactionDtls> stockTransactionDtls,String companyName)
	{
		//getHibernateTemplate().saveOrUpdateAll(stockTransactionDtls);
		//SessionFactory sessionFactory = getSessionFactory();
		Session session =  null;
		int counter = 0;
		long commitCounter = 0;
		if(companyName.equalsIgnoreCase(ApplicationConstants.RNIPL))
		{
			 session =  sessionFactory.openSession();
		}else if(companyName.equalsIgnoreCase(ApplicationConstants.NMIPL))
		{
			 session =  factory2.openSession();
		}
		if(session!=null)
		{
			
			try {
				Transaction transaction = session.beginTransaction();
				for(StockTransactionDtls objToSave : stockTransactionDtls){
					session.save(objToSave);
					counter++;
					if(counter==1000){
						counter=0;
						transaction.commit();
						transaction = session.beginTransaction();
						commitCounter+=1000;
					}
				}
				commitCounter+=counter; 
				transaction.commit();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally{
				if(session.isOpen())
					session.close();
			}
		}
		
		return commitCounter;
	}
	

}
